package com.xhsj.user.hr.process.processDefinition.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xhsj.user.hr.process.dto.PageDto;
import com.xhsj.user.hr.process.dto.processDefinition.HandleAuditDto;
import com.xhsj.user.hr.process.processDefinition.entity.TbGlobalApprovalTask;
import com.xhsj.user.hr.process.processDefinition.query.HrMessageQuery;
import com.xhsj.user.hr.process.processDefinition.service.TbGlobalApprovalTaskService;
import com.xhsj.user.security.entity.LoginUser;
import com.xhsj.user.utils.Message;
import com.xhsj.user.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Set;

//import org.springframework.security.access.prepost.PreAuthorize;


/**
 * <p>
 * 流程-任务审核表 前端控制器
 * </p>
 *
 * @author liuqinghua
 * @since Aug 31, 2020 4:15:14 PM
 */
@Slf4j
@Api(tags = "流程-任务审核表")
@RestController
@RequestMapping("/process/tbGlobalApprovalTask")
public class TbGlobalApprovalTaskController {

	@Autowired
    public TbGlobalApprovalTaskService service;


	/**
	 * 保存/修改
	 * @author liuqinghua
	 * @date Aug 31, 2020 4:15:14 PM
	 */
	@ApiOperation(value = "流程-任务审核表,保存/修改", notes = "流程-任务审核表,保存和修改方法")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
    // @PreAuthorize("hasAuthority('tb:global:approval:task:edit')")
	public Message save(@RequestBody TbGlobalApprovalTask data){
    	try {
    		service.save(data);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
        	return Message.error(1,"失败");
		}

	}

	/**
	 * 通过id删除数据
	 * @author liuqinghua
	 * @date Aug 31, 2020 4:15:14 PM
	 */
	@ApiOperation(value = "流程-任务审核表删除", notes = "通过id删除流程-任务审核表信息。")
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
    // @PreAuthorize("hasAuthority('tb:global:approval:task:delete')")
	public Message delete(@RequestParam(value="id") String id) {
		try {
			service.removeById(id);
        	return Message.success("成功");
		} catch (Exception e) {
			log.error("删除失败:{}",e);
            return Message.error(1,"失败");
		}

	}

	/**
	 * 批量删除
	 * @author liuqinghua
	 * @date Aug 31, 2020 4:15:14 PM
	 */
	@ApiOperation(value = "批量删除", notes = "批量删除。")
	@RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
	public Message batchDelete(@RequestBody List<String> list) {
		try {
			service.removeByIds(list);
    		return Message.success("成功");
		} catch (Exception e) {
    		log.error("删除失败:{}",e);
    		return Message.error(1,"失败");
		}
    }

    /**
    * 分页查询数据
    * @author liuqinghua
    * @date Aug 31, 2020 4:15:14 PM
    */
	@ApiOperation(value = "分页查询流程-任务审核表list", notes = "带参数查询,{\"page\": 1,\"size\": 5,......}。支持模糊查询")
	@RequestMapping(value="/findList",method = RequestMethod.POST)
    // @PreAuthorize("hasAuthority('tb:global:approval:task:view')")
	public Message findList(@RequestBody Map<String,Object> data) {
        try {
        	IPage<TbGlobalApprovalTask> selectPage = service.selectPage(data);
    		return Message.success("成功",selectPage.getRecords(),selectPage.getTotal(),selectPage.getPages());
        } catch (Exception e) {
			log.error("查询异常:{}",e);
    		return Message.error(1,"失败");
        }
	}

    /**
    * 查询所有数据
    * @author liuqinghua
    * @date Aug 31, 2020 4:15:14 PM
    */
	@ApiOperation(value = "查询流程-任务审核表所有数据list", notes = "查询流程-任务审核表所有数据list。")
	@RequestMapping(value="/findAll",method = RequestMethod.POST)
    // @PreAuthorize("hasAuthority('tb:global:approval:task:view')")
	public Message findAll(){
        return Message.success("成功",service.selectAll());
	}

    /**
    * 通过id获取实体类
    * @author liuqinghua
    * @date Aug 31, 2020 4:15:14 PM
    */
	@ApiOperation(value = "通过id获取流程-任务审核表实体类", notes = "通过id获取流程-任务审核表实体类。")
	@RequestMapping(value="/getById",method = RequestMethod.GET)
    // @PreAuthorize("hasAuthority('tb:global:approval:task:view')")
	public Message getById(@RequestParam(value="id") String id) {
    	return Message.success("成功",service.getById(id));
	}

    /**
    * 通过id查询详细信息
    * @author liuqinghua
    * @date Aug 31, 2020 4:15:14 PM
    */
	@ApiOperation(value = "通过id获取实体类详细信息", notes = "通过id获取实体类详细信息。")
	@RequestMapping(value="/detail",method = RequestMethod.GET)
    // @PreAuthorize("hasAuthority('tb:global:approval:task:view')")
	public Message detail(@RequestParam(value="id") String id) {
    	return Message.success("成功",service.getById(id));
	}

	/**
	 * 首页hr系统管理消息审核
	 * @author liuqinghua
	 * @date Aug 31, 2020 4:15:14 PM
	 */
	@ApiOperation(value = "首页hr系统管理消息审核", notes = "首页hr系统管理消息审核")
	@RequestMapping(value="/getHrMessage",method = RequestMethod.POST)
	public Message getHrMessage(@RequestBody PageDto data) {
        IPage<HrMessageQuery> hrMessage = service.getHrMessage(data);
        return Message.success("成功",hrMessage.getRecords(),hrMessage.getTotal(),hrMessage.getPages());
	}

    /**
     * 首页hr系统管理消息待办
     * @author liuqinghua
     * @date Aug 31, 2020 4:15:14 PM
     */
    @ApiOperation(value = "首页hr系统管理消息待办", notes = "首页hr系统管理消息待办")
    @RequestMapping(value="/getHrMessageCommission",method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('tb:global:hr:commission:view')")
    public Message getHrMessageCommission(@RequestBody PageDto data) {
        IPage<HrMessageQuery> hrMessage = service.getHrMessageCommission(data);
        return Message.success("成功",hrMessage.getRecords(),hrMessage.getTotal(),hrMessage.getPages());
    }


	/**
	 * 首页hr系统管理消息审核数
	 * @author liuqinghua
	 * @date Aug 31, 2020 4:15:14 PM
	 */
	@ApiOperation(value = "首页hr系统管理消息审核数", notes = "首页hr系统管理消息审核数")
	@RequestMapping(value="/getHrMessageNum",method = RequestMethod.GET)
	public Integer getHrMessageNum() {
        LoginUser loginAppUser = UserUtils.getLoginAppUser();
        Set<String> permissions = loginAppUser.getPermissions();
        Integer commissionNum = service.getHrMessageNum();
		if (commissionNum != null && permissions != null) {
        if (permissions.contains("tb:global:hr:commission:num")){
            Integer hrMessageCommissionNum = this.getHrMessageCommissionNum();
            commissionNum = hrMessageCommissionNum+commissionNum;
        }
		}
        return commissionNum;
	}

	/**
	 * 首页hr系统管理消息代办数
	 * @author liuqinghua
	 * @date Aug 31, 2020 4:15:14 PM
	 */
	@ApiOperation(value = "首页hr系统管理待办数", notes = "首页hr系统管理待办数")
	@RequestMapping(value="/getHrMessageCommissionNum",method = RequestMethod.GET)
	@PreAuthorize("hasAuthority('tb:global:hr:commission:num')")
	public Integer getHrMessageCommissionNum() {
		Integer commissionNum = service.getHrMessageCommissionNum();
		return commissionNum;
	}

}

